package com.view;

import com.domain.AdminBean;
import com.domain.Grade;
import com.domain.StudentBean;
import com.service.AdminService;
import com.service.GradeService;
import com.service.StudentService;
import com.tools.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Scanner;

public class MainView {
    private StudentService studentService = new StudentService();

    public void run() {
        Scanner sc = new Scanner(System.in);
        boolean flag = true;

        login();

        while (flag) {
            System.out.println("----------------欢迎使用学生管理系统--------------------");
            System.out.println("-------------------请选择操作选项-----------------------");
            System.out.println("1.添加信息\t2.删除信息\t3.修改信息\t4.查询信息\t5.退出系统");
            System.out.print("请选择：");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    //添加信息
                    addMsg();
                    break;
                case 2:
                    //删除信息
                    deleteMsg();
                    break;
                case 3:
                    //修改信息
                    updateMsg();
                    break;
                case 4:
                    //查询信息
                    selectMsg();
                    break;
                case 5:
                    //退出系统
                    flag = false;
                    System.out.println("再见！欢迎下次使用！");
                    break;
                default:
                    flag = false;
                    System.out.println("输入错误，退出系统！");
                    break;

            }
        }
    }

    //删除信息
    private void deleteMsg() {
        Scanner sc = new Scanner(System.in);
        System.out.println("-------------------请选择操作选项-----------------------");
        System.out.println("1.删除学生信息(先删除成绩单)\t2.删除成绩单\t3.删除管理员信息");
        System.out.print("请选择：");
        int num = sc.nextInt();
        switch (num) {
            case 1:
                //删除学生信息
                deleteStudent();
                break;
            case 2:
                //删除学生成绩
                deleteGrade();
                break;
            case 3:
                //删除管理员信息
                deleteAdmin();
                break;
            default:
                break;
        }

    }

    /**
     * 删除学生成绩
     */
    private void deleteGrade() {
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入要删除成绩单号：");
            int gradeId = sc.nextInt();

            GradeService.deleteGrade(gradeId);
        }

    /**
     * 删除管理员信息
     */
    private void deleteAdmin() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要删除管理员的id：");
        int adminId = sc.nextInt();

        AdminService.deleteAdmin(adminId);
        System.out.println("删除管理员信息成功！");
    }

    /**
     * 添加信息
     */
    private void addMsg() {
        Scanner sc = new Scanner(System.in);
        System.out.println("-------------------请选择操作选项-----------------------");
        System.out.println("1.添加学生信息\t2.添加管理员信息");
        System.out.print("请选择：");
        int num = sc.nextInt();
        switch (num) {
            case 1:
                //添加学生信息
                addStudent();
                //学生成绩
                addGrade();
                break;
            case 2:
                //添加管理员信息
                addAdmin();
                break;
            default:
                break;
        }
    }

    /**
     * 添加管理员信息
     */
    private void addAdmin() {
        Scanner sc = new Scanner(System.in);
        AdminBean admin = new AdminBean();
        System.out.print("请输入要添加的管理员id：");
        admin.setLoginId(sc.nextInt());
        System.out.print("请输入要添加的管理员的密码：");
        admin.setLoginPwd(sc.next());

        AdminService.addAdmin(admin);
        System.out.println("添加管理员信息成功!");
    }

    /**
     * 删除学生信息
     */
    private void deleteStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要删除的学生id：");
        int id = sc.nextInt();
        StudentService.deleteStudent(id);
        System.out.println("删除学生信息成功!");
    }

    /**
     * 添加学生成绩
     */
    private void addGrade() {
        Scanner in = new Scanner(System.in);
        Grade grade = new Grade();
        System.out.print("请输入要添加的学生学号：");
        grade.setGradeId(in.nextInt());
        System.out.print("请输入要添加的学生语文成绩：");
        grade.setChinese(in.nextInt());
        System.out.print("请输入要添加的学生数学成绩：");
        grade.setMath(in.nextInt());
        System.out.print("请输入要添加的学生英语成绩：");
        grade.setEnglish(in.nextInt());
        System.out.print("请输入要添加的学生Java成绩：");
        grade.setJava(in.nextInt());
        System.out.print("请输入要添加的学生Oracle成绩：");
        grade.setOracle(in.nextInt());

        StudentService.addGrade(grade);
        System.out.println("添加学生成绩成功！");
    }

    /**
     * 添加学生信息
     */
    private void addStudent() {
        /*
        1.获取用户输入、封装到bean中
        2.调回service的addStudent()方法完成添加功能
        */
        Scanner in = new Scanner(System.in);
        StudentBean studentBean = new StudentBean();
        System.out.print("请输入要添加的学生id：");
        studentBean.setSId(in.nextInt());
        System.out.print("请输入要添加的学生姓名：");
        studentBean.setSName(in.next());
        System.out.print("请输入要添加的学生年龄：");
        studentBean.setSAge(in.nextInt());
        System.out.print("请输入要添加的学生性别：");
        studentBean.setSSex(in.next());
        System.out.print("请输入要添加的学生地址：");
        studentBean.setSAddress(in.next());
        System.out.print("请输入要添加的学生生日：");
        studentBean.setSDate(in.next());
        System.out.print("请输入要添加的学生成绩单号：");
        studentBean.setGradeId(in.nextInt());

        StudentService.addStudent(studentBean);
        System.out.println("添加学生信息成功！");

    }

    /**
     * 查询总菜单
     */
    private void selectMsg() {
        System.out.println("-------------------请选择操作选项-----------------------");
        System.out.print("1.查询所有学生信息\t2.按照成绩单号查询成绩单\t3.查询管理员信息");
        Scanner in = new Scanner(System.in);
        int op = in.nextInt();
        switch (op) {
            case 1:
                selectAll();//查询所有学生信息
                break;
            case 2:
                selectId();//按照成绩单号查询成绩单
                break;
            case 3:
                selectAd();//查询管理员
                break;
            default:
                System.out.println("输入错误！");
        }
    }

    /**
     * 查询管理员信息
     */
    private void selectAd() {
        List<AdminBean> AdminList = AdminService.selectAd();

        print3(AdminList);
    }

    /**
     * 按按照成绩单号查询成绩单
     */
    public void selectId() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要查询学生的成绩单号：");
        int gra = sc.nextInt();
        List<Grade> GradeBean = StudentService.selectId(gra);
        //打印 print()
        print2(GradeBean);
    }

    /**
     * 查询学生所有信息
     */
    public void selectAll() {
        List<StudentBean> StudentList = StudentService.selectAll();
        //打印print
        print(StudentList);
    }

    /**
     * 打印学生所有信息
     *
     * @param StudentList
     */
    private void print(List<StudentBean> StudentList) {
        System.out.println("ID\t姓名\t年龄\t性别\t地址\t\t\t时间\t成绩单号");
        for (StudentBean st : StudentList) {
            System.out.println(st.getSId() + "\t" + st.getSName() + "\t" + st.getSAge() + "\t\t" + st.getSSex() + "\t\t" +
                    st.getSAddress() + "\t" + st.getSDate() + "\t\t" + st.getGradeId());
        }
    }

    /**
     * 打印学生成绩单
     *
     * @param GradeList
     */
    private void print2(List<Grade> GradeList) {
        System.out.println("成绩单号\t\t语文\t数学\t英语\tJava\tOracle");
        for (Grade or : GradeList) {
            System.out.println(or.getGradeId() + "\t\t\t" + or.getChinese() + "\t" + or.getMath() + "\t" +
                    or.getMath() + "\t" + or.getJava() + "\t" + or.getOracle());
        }
    }

    /**
     * 打印管理员信息
     *
     * @param adminList
     */
    private void print3(List<AdminBean> adminList) {
        System.out.println("账号\t密码");
        for (AdminBean ad : adminList) {
            System.out.println(ad.getLoginId() + "\t" + ad.getLoginPwd());
        }
    }

    /**
     * 修改信息
     */
    private void updateMsg() {
        Scanner sc = new Scanner(System.in);
        System.out.println("-------------------请选择操作选项-----------------------");
        System.out.println("1.修改学生信息\t2.修改学生成绩\t3.修改管理员密码");
        int num = sc.nextInt();
        switch (num) {
            case 1:
                //修改学生信息
                System.out.println("-------------------请选择操作选项-----------------------");
                System.out.println("1.修改学生姓名\t2.修改学生年龄\t3.修改学生性别\t4.修改学生住址\t5.修改学生生日");
                int num1 = sc.nextInt();
                switch (num1) {
                    case 1:
                        //修改学生姓名
                        updateStudentName();
                        break;
                    case 2:
                        //修改学生年龄
                        updateStudentAge();
                        break;
                    case 3:
                        //修改学生性别
                        updateStudentSex();
                        break;
                    case 4:
                        //修改学生住址
                        updateStudentAddress();
                        break;
                    case 5:
                        //修改学生生日
                        updateStudentDate();
                        break;
                    default:
                        break;
                }
                break;
            case 2:
                //修改学生成绩
                updateGrade();
                break;
            case 3:
                //修改管理员密码
                updateAdminPassword();
                break;
            default:
                break;

        }
    }

    /**
     * 修改管理员信息
     */
    private void updateAdminPassword() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入管理员ID：");
        int adminId = sc.nextInt();
        System.out.print("请输入新密码：");
        String adminPwd = sc.next();

        AdminService.updateAdminPassword(adminId,adminPwd);
        System.out.println("修改管理员信息成功!");
    }

    /**
     * 修改学生成绩
     */
    private void updateGrade() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入学生成绩单号");
        int gradeId = sc.nextInt();
        System.out.print("请输入要添加的学生语文成绩：");
        int chinese = sc.nextInt();
        System.out.print("请输入要添加的学生数学成绩：");
        int math =sc.nextInt();
        System.out.print("请输入要添加的学生英语成绩：");
        int english =sc.nextInt();
        System.out.print("请输入要添加的学生Java成绩：");
        int java =sc.nextInt();
        System.out.print("请输入要添加的学生Oracle成绩：");
        int oracle =sc.nextInt();

        GradeService.updateGrade(gradeId,chinese,math,english,java,oracle);
        System.out.println("修改学生成绩成功!");
    }

    /**
     * 修改学生生日
     */
    private void updateStudentDate() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入学生学号:");
        int id = sc.nextInt();
        System.out.print("请输入学生生日:");
        String birth = sc.next();
        studentService.updateStudentDate(id, birth);
        System.out.println("修改学生生日成功!");
    }

    /**
     * 修改学生住址
     */
    private void updateStudentAddress() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入学生学号:");
        int id = sc.nextInt();
        System.out.print("请输入学生住址:");
        String address = sc.next();
        studentService.updateStudentAddress(id, address);
        System.out.println("修改学生住址成功!");
    }

    /**
     * 修改学生性别
     */
    private void updateStudentSex() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入学生学号:");
        int id = sc.nextInt();
        System.out.print("请输入学生性别(男/女):");
        String sex = sc.next();
        studentService.updateStudentSex(id, sex);
        System.out.println("修改学生性别成功!");
    }

    /**
     * 修改学生年龄
     */
    private void updateStudentAge() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入学生学号:");
        int id = sc.nextInt();
        System.out.print("请输入学生年龄:");
        int age = sc.nextInt();
        studentService.updateStudentAge(id, age);
        System.out.println("修改学生年龄成功!");
    }

    /**
     * 修改学生姓名
     */
    private void updateStudentName() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入学生学号:");
        int id = sc.nextInt();
        System.out.print("请输入学生姓名:");
        String name = sc.next();
        studentService.updateStudentName(id, name);
        System.out.println("修改学生姓名成功!");
    }

    /**
     * 登录学生管理系统
     */
    private void login() {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        while (flag) {
            System.out.print("请输入用户名：");
            int loginId = sc.nextInt();
            System.out.print("请输入密码：");
            String loginPwd = sc.next();

            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;

            try {
                conn = DBUtil.getConnection();
                String sql = "select * from admin where loginId = ? and loginPwd = ?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1, loginId);
                ps.setString(2, loginPwd);
                rs = ps.executeQuery();
                if (rs.next()) {
                    flag = false;
                    System.out.println("登陆成功！");
                } else {
                    System.out.println("输入错误，请重新输入！");
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(conn, ps, rs);
            }
        }
    }
}
